import { createRouter, createWebHistory } from 'vue-router'
import Login from '../views/Login.vue'
import Register from '../views/Register.vue'
import Home from '../views/Home.vue'
import Profile from '../views/Profile.vue'
import Discussion from '../views/Discussion.vue'
import DiscussionDetail from '../views/DiscussionDetail.vue'
import Problems from '../views/Problems.vue'
import ProblemDetail from '../views/ProblemDetail.vue'
import ProblemEdit from '../views/ProblemEdit.vue'
import Learning from '../views/Learning.vue'

const routes = [
  {
    path: '/',
    redirect: '/home'  // 改为重定向到首页
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/register',
    name: 'Register',
    component: Register
  },
  {
    path: '/home',
    name: 'Home',
    component: Home,
    meta: { requiresAuth: true }  // 首页也需要登录
  },
  {
    path: '/profile',
    name: 'Profile',
    component: Profile,
    meta: { requiresAuth: true }
  },
  {
    path: '/discussion',
    name: 'Discussion',
    component: Discussion,
    meta: { requiresAuth: true }
  },
  {
    path: '/discussion/:id',
    name: 'DiscussionDetail',
    component: DiscussionDetail,
    meta: { requiresAuth: true }
  },
  {
    path: '/problems',
    name: 'Problems',
    component: Problems,
    meta: { requiresAuth: true }
  },
  {
    path: '/problem/:id',
    name: 'ProblemDetail',
    component: ProblemDetail,
    meta: { requiresAuth: true }
  },
  {
    path: '/problem/new',
    name: 'ProblemCreate',
    component: ProblemEdit,
    meta: { requiresAuth: true, requiresAdmin: true }
  },
  {
    path: '/problem/:id/edit',
    name: 'ProblemEdit',
    component: ProblemEdit,
    meta: { requiresAuth: true, requiresAdmin: true }
  },
  {
    path: '/my-learning',
    name: 'MyLearning',
    component: () => import('../views/MyLearning.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/my-posts',
    name: 'MyPosts',
    component: () => import('../views/MyPosts.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/learning',
    name: 'Learning',
    component: Learning,
    meta: { requiresAuth: true }
  },
  {
    path: '/learning/course/:courseId',
    name: 'CourseDetail',
    component: () => import('../views/CourseDetail.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/learning/video/:videoId',
    name: 'VideoPlayer',
    component: () => import('../views/VideoPlayer.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/learning/manage',
    name: 'LearningManage',
    component: () => import('../views/LearningManage.vue'),
    meta: { requiresAuth: true, requiresAdmin: true }
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫：未登录时跳转到登录页
router.beforeEach(async (to, from, next) => {
  const token = sessionStorage.getItem('token')
  const userInfo = JSON.parse(sessionStorage.getItem('userInfo') || 'null')
  const isAdmin = userInfo?.role === 'admin'
  
  // 如果已登录但 WebSocket 未连接，自动连接
  if (token && to.meta.requiresAuth) {
    const { wsManager } = await import('../utils/websocket')
    if (!wsManager.isConnected()) {
      wsManager.connect(token)
    }
  }
  
  // 如果需要登录且没有token
  if (to.meta.requiresAuth && !token) {
    next('/login')
  } 
  // 如果需要管理员权限但不是管理员
  else if (to.meta.requiresAdmin && !isAdmin) {
    alert('无权限访问此页面')
    next('/problems')
  }
  // 如果已登录访问登录页，重定向到首页
  else if (token && (to.path === '/login' || to.path === '/register')) {
    next('/home')
  } 
  else {
    next()
  }
})

export default router





